package com.tianzj.boot.mvc.controller.system;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.tianzj.boot.mvc.entity.system.SysMenu;
import com.tianzj.boot.mvc.service.system.SysMenuService;
import com.tianzj.boot.util.AjaxJson;
import com.tianzj.boot.util.JsonUtil;
import com.tianzj.boot.util.tree.Tree;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("sysMenu")
@Slf4j
public class SysMenuController {

	@Autowired
	private SysMenuService sysMenuService;
	
	/**
	 * 跳转到图标页面
	 * @return
	 */
	@GetMapping("/icon")
    public ModelAndView icon(String id,Model model){
		model.addAttribute("iconId", id);
    	return new ModelAndView("menu/icon");
    }
	
	/**
	 * 跳转到菜单页面
	 * @return
	 */
	@RequestMapping("menuList")
	public ModelAndView menuList(){
		return new ModelAndView("menu/menuList");
	}
	
	/**
	 * 新增菜单
	 * @param sysMenu
	 * @return
	 */
	@PostMapping("addSysMenu")
	public ModelAndView addSysMenu(SysMenu sysMenu){
		try {
			sysMenu.setCreateDate(System.currentTimeMillis());
			sysMenu.setCreateUser(1L);
			sysMenuService.saveSysMenu(sysMenu);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return new ModelAndView("redirect:/sysMenu/menuList");
	}
	
	/**
	 * 新增菜单
	 * @param sysMenu
	 * @return
	 */
	@PostMapping("editSysMenu")
	public ModelAndView editSysMenu(SysMenu sysMenu){
		try {
			sysMenu.setUpdateDate(System.currentTimeMillis());
			sysMenu.setUpdateUser(1L);
			sysMenuService.updateSysMenu(sysMenu);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return new ModelAndView("redirect:/sysMenu/menuList");
	}
	/**
	 * 菜单列表页面
	 * @return
	 */
	@GetMapping("getMenuList")
	public String  getMenuList(){
		List<SysMenu> sysMenuList= sysMenuService.getSysMenuList();
		String json="";
		try {
			json = JsonUtil.objectToJson(sysMenuList).toString();
		} catch (IOException e) {
			log.error(e.getMessage());
		}
		json=json.replaceAll("id","menuId");
		return json;
	}
	
	/**
	 * 根据id查询
	 * @param id
	 * @return
	 */
	@PostMapping("getSysMenuById")
	public SysMenu getSysMenuById(Long id){
		SysMenu sysMenu=sysMenuService.findById(id);
		if(sysMenu!=null&&sysMenu.getParentId()!=null){
			SysMenu parentMenu = sysMenuService.findById(sysMenu.getParentId());
			if(parentMenu!=null){
				sysMenu.setParentName(parentMenu.getName());
			}
		}
		return sysMenu;
	}
	
	/**
	 * 根据id删除查询
	 * @param id
	 * @return
	 */
	@PostMapping("removeSysMenuById")
	public AjaxJson removeSysMenuById(Long id){
		AjaxJson ajaxJson = new AjaxJson();
		try {
			List<SysMenu> sysMenus=sysMenuService.findByParentId(id);
			if(sysMenus!=null&&sysMenus.size()!=0){
				ajaxJson.setMsg("包含下级菜单,请先删除下级菜单！");
			}else{
				sysMenuService.deleteSysMenuById(id);
				ajaxJson.setMsg("删除成功");
			}
		} catch (Exception e) {
			ajaxJson.setMsg("删除失败");
		}
		return ajaxJson;
	}
	
	
	@PostMapping("getMenusTree")
	public Tree<SysMenu> getMenusTree(Long id) {
		Tree<SysMenu> tree = new Tree<SysMenu>();
		tree = sysMenuService.getTree(id);
		System.out.println(tree.toString());
		return tree;
	}
	
}
